package com.iye.puretennis.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import com.iye.puretennis.LoginActivity;
import com.iye.puretennis.R;
import com.iye.puretennis.custom.CustomApplication;
import com.iye.puretennis.domain.UserToken;
import com.iye.puretennis.helper.Constant;
import com.iye.puretennis.helper.DBManager;

/**
 * Created with IntelliJ IDEA.
 * User: jing wu
 * Date: 2015/1/7
 * Time: 16:04
 */
public class SplashActivity extends Activity {

    private final int SPLASH_DISPLAY_TIME = 4000; // 延迟四秒

    private DBManager dbManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splash);
        CustomApplication application = (CustomApplication) getApplication();
        application.setHuaWenHuPo(Typeface.createFromAsset(getAssets(), Constant.DEFAULT_FONT));

        dbManager = new DBManager(this);

        new Handler().postDelayed(new Runnable() {
            public void run() {
                UserToken userToken = dbManager.retrieveCurrentUser();
                Intent mainIntent;
                if (userToken == null) {
                    mainIntent = new Intent(SplashActivity.this, LoginActivity.class);
                } else {
                    if (userToken.isSetup()) {
                        mainIntent = new Intent(SplashActivity.this, MainTabsActivity.class);
                    } else {
                        mainIntent = new Intent(SplashActivity.this, ProfileActivity.class);
                    }
                }

                SplashActivity.this.startActivity(mainIntent);
                SplashActivity.this.finish();
            }

        }, SPLASH_DISPLAY_TIME);

    }
}
